package com.woniu.liuzhougov.WorkPlan.service.iml;

import com.woniu.liuzhougov.WorkPlan.Enum.PlanEnum;
import com.woniu.liuzhougov.WorkPlan.Enum.PlanServiceEnum;
import com.woniu.liuzhougov.WorkPlan.Exception.PlanException;
import com.woniu.liuzhougov.WorkPlan.dao.PlanMapper;
import com.woniu.liuzhougov.WorkPlan.dto.PlanDTO;
import com.woniu.liuzhougov.WorkPlan.pojo.Plan;
import com.woniu.liuzhougov.WorkPlan.service.PlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: junzhao
 * @Date: 2019-04-19 16:12
 */
@Service
public class PlanServiceImpl implements PlanService {

    @Resource
    private PlanMapper planMapper;

    @Resource
    private Plan plan;

    @Override
    public String deleteByPrimaryKey(Integer planId) {
        planMapper.deleteByPrimaryKey(planId);
        return PlanServiceEnum.OK.getMessage();
    }

    /**
     * @Description: 添加工作计划
     * @Author: JunZhao
     * @Date: 2019-04-25 19:15
     * @Param: [plan]
     * @Return: java.lang.String
     */
    @Override
    public String insertSelective(Plan plan) {
        plan.setPlanRelease(PlanEnum.PLAN_NEW_RELEASE.getCode());
        planMapper.insert(plan);
        return PlanServiceEnum.OK.getMessage();
    }

    /**
     * @Description:
     * @Author: JunZhao
     * @Date: 2019-04-25 17:10
     * @Param: [planId]
     * @Return: com.woniu.liuzhougov.WorkPlan.dto.PlanDTO
     */
    @Override
    public PlanDTO selectByPrimaryKey(Integer planId) {
        plan = planMapper.selectByPrimaryKey(planId);
        PlanDTO planDTO = new PlanDTO();
        BeanUtils.copyProperties(plan, planDTO);
        if (planDTO.getPlanTypeId() == 1) {
            planDTO.setPlanTypeName(PlanEnum.PLAN_TYPE_NAME_YEAR.getMessage());
        } else if (planDTO.getPlanTypeId() == 2) {
            planDTO.setPlanTypeName(PlanEnum.PLAN_TYPE_NAME_MOUTH.getMessage());
        }
        return planDTO;
    }

    /**
     * 更新工作计划内容
     *
     * @param planDTO
     */
    @Override
    public String updateByPrimaryKeySelective(PlanDTO planDTO) {
        BeanUtils.copyProperties(planDTO, plan);
        planMapper.updateByPrimaryKeySelective(plan);
        return PlanServiceEnum.OK.getMessage();
    }

    /**
     *
     * @param plan
     * @return
     */
    @Override
    public String updateByPrimaryKey(Plan plan) {
        planMapper.updateByPrimaryKey(plan);
        return PlanServiceEnum.OK.getMessage();
    }

    /**
     * 根据参数类型动态查询数据
     *
     * @param planDTO
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public List<PlanDTO> queryPlanByPage(PlanDTO planDTO, Integer page, Integer pageSize) {
        BeanUtils.copyProperties(planDTO, plan);
        List<PlanDTO> planList = planMapper.queryPlanByPage(plan);
        for (PlanDTO plan : planList) {
            if (plan.getPlanRelease() == 1) {
                plan.setPlanReleaseStatus(PlanEnum.PLAN_NOT_RELEASE.getMessage());
            } else {
                plan.setPlanReleaseStatus(PlanEnum.PLAN_OK_RELEASE.getMessage());
            }
        }
        return planList;
    }

    /**
     *  查询工作计划详情 以及发布者名称
     * @param planId
     * @return
     */
    @Override
    public PlanDTO selectByPrimaryKeyAndUserId(Integer planId) {
        PlanDTO planDTO = planMapper.selectByPrimaryKeyAndUserId(planId);
        if (planDTO.getPlanTypeId() == 1) {
            planDTO.setPlanTypeName(PlanEnum.PLAN_TYPE_NAME_YEAR.getMessage());
        } else if (planDTO.getPlanTypeId() == 2) {
            planDTO.setPlanTypeName(PlanEnum.PLAN_TYPE_NAME_MOUTH.getMessage());
        }
        return planDTO;
    }

    /**
     * 发布工作计划
     * @param planDTO
     * @return
     */
    @Override
    public String planRelease(PlanDTO planDTO) {
        plan = planMapper.selectByPrimaryKey(planDTO.getPlanId());
        if (plan.getPlanRelease() == 2) {
            throw new PlanException(PlanEnum.PLAN_RELEASE_ERROR);
        }
        //获取当前系统时间
        planDTO.setPlanTime(new Date());
        //设置状态为2
        planDTO.setPlanRelease(2);
        BeanUtils.copyProperties(planDTO, plan);
        planMapper.updateByPrimaryKeySelective(plan);
        return PlanServiceEnum.OK.getMessage();
    }
}
